
% generate_ref_banana
% Christopher J. Whalen
% University of Illinois
% 10/19/04 - Date Created
% 10/19/04 - Last Modified

% Purpose - to create a reference banana

function [banana_list] = gen_ref_banana(choice,n_x_ref,n_y_ref,n_z_ref,banana_radius,max_banana_girth,banana_list,banana_brik);

fprintf('\nGenerating Reference Banana...\n');

fuzz_thresh = 0.1; % Setting threshold for 'fuzziness' to appear.
% Approximating Middle Banana with Parabolas
% Lower Bound
A = [0^2 0^2 1;(n_x_ref/2)^2 n_x_ref/2 1;n_x_ref^2 n_x_ref 1];
B = [n_z_ref (n_z_ref - banana_radius - max_banana_girth) n_z_ref]';
a = (A\B)'; % coefficient matrix
% lower bound parabola equation
% fprintf('\nLower Bound Parabola: y = %fx^2 + %fx + %f', a(1),a(2),a(3));
% Upper Bound
C = [0^2 0^2 1;(n_x_ref/2)^2 n_x_ref/2 1;n_x_ref^2 n_x_ref 1];
D = [n_z_ref (n_z_ref - banana_radius + max_banana_girth) n_z_ref]';
b = (C\D)'; % coefficient matrix
% upper bound parabola equation
% fprintf('\nUpper Bound Parabola: y = %fx^2 + %fx + %f', b(1),b(2),b(3));
% upr_bnd_parab = b(1)*x^2 + b(2)*x + b(3);
% Mean Parabola
E = [0^2 0^2 1;(n_x_ref/2)^2 n_x_ref/2 1;n_x_ref^2 n_x_ref 1];
F = [n_z_ref (n_z_ref - banana_radius) n_z_ref]';
c = (E\F)'; % coefficient matrix
% mean parabola equation
% fprintf('\nMean Parabola: y = %fx^2 + %fx + %f', c(1),c(2),c(3));
% mean_parab = c(1)*x^2 + c(2)*x + c(3);


% Generate the banana list
% ref_banana(:,n_y_ref/2,:) = 1;
% i_y = n_y_ref/2;
i_banana_pt = 1; % banana counter
i_y_min = fix(n_y_ref/2 - max_banana_girth ); % rounds to zero: lower y bound on banana
i_y_max = ceil((n_y_ref/2) + max_banana_girth); % rounds to infinity: upper y bound on banana
i_z_min = fix(n_z_ref - banana_radius - max_banana_girth); % minimum z value of banana
for i_y=i_y_min:i_y_max
	for i_x=1:n_x_ref
        z_mid = c(1)*(i_x^2) + c(2)*i_x + c(3);
        z_max = b(1)*(i_x^2) + b(2)*i_x + b(3);
        banana_girth = z_max - z_mid;
        del_z = sqrt(banana_girth^2 - (i_y - (n_y_ref/2))^2);
        if isreal(del_z)
            for i_z=i_z_min:n_z_ref
                if (i_z <=  z_mid + del_z) & (i_z >= z_mid - del_z)
                    banana_list(i_banana_pt,1:3) = [i_x,i_y,i_z];
                    banana_brik(i_x,i_y,i_z) = 1; % default banana value
                    i_banana_pt = i_banana_pt + 1;
                end     
            end
        end
	end
end


if choice.banana_type == 1
    banana_list(i_banana_pt:size(banana_list,1),:)=[];
elseif choice.banana_type == 2 | choice.banana_type == 3
    banana_brik = smooth3(banana_brik,'gaussian',[7 7 7]);
    banana_brik = smooth3(banana_brik,'gaussian',[7 7 7]); %double smoothing to make very 'fuzzy'
    banana_brik = smooth3(banana_brik,'gaussian',[7 7 7]); %triple smoothing to make very 'fuzzy'
    fuzz_pt = 1; %counter
    for i=1:n_x_ref
        for j=1:n_y_ref
            for k=1:n_z_ref
                if banana_brik(i,j,k)>fuzz_thresh
                    banana_list(fuzz_pt,:) = [i j k banana_brik(i,j,k)];
                    fuzz_pt = fuzz_pt + 1;
                end
            end
        end
    end
    banana_list(fuzz_pt:size(banana_list,1),:)=[]; % removing all nonzero rows
end



% % Displaying a random Number of Banana Points
% num_rnd_pts = 1000;
% random_banana_num_list = randint(num_rnd_pts,1,[0,length(banana_list)]);
% random_banana_counter = 1;
% random_banana = zeros(num_rnd_pts,4);
% for i=1:num_rnd_pts
%     random_banana(random_banana_counter,:) = banana_list(random_banana_num_list(i),:);
%     random_banana_counter = random_banana_counter + 1;
% end
% scatter3(random_banana(:,1), random_banana(:,2), random_banana(:,3), 2.5, 'r','filled'); 



fprintf('Reference Banana Created\n');